package app.qrcode.browser.ykpixabay.ui.login

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavController
import androidx.navigation.Navigation
import app.qrcode.browser.ykpixabay.R
import app.qrcode.browser.ykpixabay.logic.dao.KeyDao
import kotlinx.coroutines.delay

/**
 * Created by YiGo on 2022/4/18.
 */
class SplashFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_splash, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val controller: NavController = Navigation.findNavController(
                    this@SplashFragment.requireActivity(),
                    R.id.fragmentContainerView
                )
        lifecycleScope.launchWhenResumed {
            delay(900)
            if (!KeyDao.isKeySaved()){
                controller.navigate(R.id.action_splashFragment_to_welcomeFragment)
            }
            else{
                controller.navigate(R.id.action_splashFragment_to_viewPicFragment)
            }
        }
    }
}